package com.zzqstart.bannerservice.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzqstart.bannerservice.entity.Banner;
import com.zzqstart.bannerservice.entity.QueryBanner;
import com.zzqstart.bannerservice.service.BannerService;
import com.zzqstart.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: BannerAdminController
 * Package: com.zzqstart.bannerservice.controller
 * Description:
 *
 * @Author zzqstart
 * @Create 2023-03-06 20:58
 * @Version 1.0
 */
@Api(tags = "后台轮播图管理")
@RestController
@RequestMapping("/bannerservice/back")
public class BannerAdminController {

    @Autowired
    private com.zzqstart.bannerservice.service.BannerService BannerService;

    @ApiOperation(value = "分页显示轮播图信息")
    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable("page") Long page, @PathVariable("limit") Long limit,@RequestBody(required = false) QueryBanner banner){
        Map<String,Object> map =  BannerService.pageBanner(page,limit,banner);
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加轮播图")
    @CachePut(key = "'selectIndexList'", value = "banner")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody Banner Banner){
        BannerService.saveOrUpdate(Banner);
        return R.ok();
    }

    @CacheEvict(key = "'selectIndexList'", value = "banner")
    @ApiOperation(value = "删除轮播图")
    @DeleteMapping("/delBanner/{id}")
    public R delBanner(@PathVariable String id){
        Banner banner = new Banner();
        banner.setId(id);
        banner.setIsDeleted(1);
        BannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取轮播图")
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id){
        Banner banner = BannerService.getById(id);
        return R.ok().data("banner",banner);
    }
}
